package com.example.databindingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.databindingdemo.R;
import com.example.databindingdemo.databinding.ActivityTwoWayBindingViewmodelBinding;
import com.example.databindingdemo.viewmodel.TwoWayBindingViewModelField;

/**
 * @author: njb
 * @date: 2022/9/18 16:30
 * @desc:
 */
public class TwoWayBindingViewModelActivity extends AppCompatActivity {
    ActivityTwoWayBindingViewmodelBinding twoWayBindingBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
    }


    private void initView() {
        twoWayBindingBinding = DataBindingUtil.setContentView(this, R.layout.activity_two_way_binding_viewmodel);
        //使用ViewModel实现双向绑定
        //twoWayBindingBinding.setViewModel(new TwoWayBindingViewModel());
        //使用ObservableField优化双向绑定
        twoWayBindingBinding.setViewModel(new TwoWayBindingViewModelField());
    }

    private void initListener() {
        twoWayBindingBinding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TwoWayBindingViewModelActivity.this, RecyclerViewActivity.class));
            }
        });
    }
}
